package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps2d.model.LatLng;
import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.HoveringScrollview;
import com.stx.xhb.mylibrary.XBanner;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.OpenExternalMapAppUtils;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.activitity.minemodule.MyFaBuActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.javaBean.CompanyInfoDetailBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

public class CompanyInfoDetailActivity extends BaseActivity implements View.OnClickListener, ContributorView, XBanner.XBannerAdapter {

    private Context context;
    private ContributorPresenter contributorPresenter;
    private String id;
    private final String COMPANYDETAILFLAG = "COMPANYDETAILFLAG";
    private final String COMPANYRELEASEDETAILFLAG = "COMPANYRELEASEDETAILFLAG";
    private CompanyInfoDetailBean companyInfoDetailBean;
    private XBanner fragment_qyxq_xBanner;//轮播控件
    private TextView fragment_qyxq_title;//标题
    private TextView fragment_qyxq_suoshu;//所属楼宇
    private TextView fragment_qyxq_guimo;//企业规模
    private TextView fragment_qyxq_wangzhan;//企业网站
    private TextView fragment_qyxq_addres;//企业地址
    private TextView fragment_qyxq_JYcontent;//经营范围
    private TextView fragment_qyxq_JJcontent;//企业简介
    private ImageView fragment_qyxq_back;//返回按钮
    private RelativeLayout layout_solve;
    private TextView tv_delete;
    private LinearLayout layout_person;
    private TextView tv_solve;
    private TextView tv_name;
    private TextView tv_sex;
    private TextView tv_phone;
    private final String DELETEHOUSEDELETEFLAG="DELETEHOUSEDELETEFLAG";
    private MyToast myToast;
    private LinearLayout ll_mine_nav;
    private LinearLayout ll_mine_nav_replace;
    private RelativeLayout layout_nav_back;
    private HoveringScrollview refreshscrollView;
    private int layout_headTop;
    private LinearLayout ll_item;
    private TextView tv_headtitle;
    private RelativeLayout layout_address;
    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.companyinfodetail_layout);
        context = this;
        initView();
        myApplication=(MyApplication) getApplication();
        myToast=new MyToast(context);
        Intent intent=getIntent();
        id = intent.getStringExtra("id");
        String cidNumber=intent.getStringExtra("cid");
        String className=intent.getStringExtra("classname");
        contributorPresenter = new ContributorPresenter(this, context);
        if(className.equals(CompanyInfoActivity.class.getSimpleName()) || className.equals(CompanySearchActivity.class.getSimpleName()))
        {
            contributorPresenter.getCompanyDetail(cidNumber, id, COMPANYDETAILFLAG);
        }else if(className.equals(MyFaBuActivity.class.getSimpleName()))
        {
            layout_solve.setVisibility(View.VISIBLE);
            tv_delete.setVisibility(View.VISIBLE);
            layout_person.setVisibility(View.VISIBLE);
            contributorPresenter.getCompanyReleaseDetail(id,COMPANYRELEASEDETAILFLAG);
        }

        refreshscrollView.setOnScrollListener(new HoveringScrollview.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                layout_headTop = ll_mine_nav_replace.getTop() + context.getResources().getDimensionPixelOffset(R.dimen.fivedp);;
                int commentTop = ll_item.getTop()/3;
                if (scrollY > layout_headTop) {
                    if (layout_nav_back.getParent() != ll_mine_nav) {
                        ll_mine_nav_replace.removeView(layout_nav_back);
                        ll_mine_nav.addView(layout_nav_back);
                        tv_headtitle.setText("企业信息详情");
                    }
                } else {

                    if (layout_nav_back.getParent() != ll_mine_nav_replace) {
                        ll_mine_nav.removeView(layout_nav_back);
                        ll_mine_nav_replace.addView(layout_nav_back);
                        tv_headtitle.setText("企业信息详情");
                    }
                }

                int alph = (int) (Tools.div(255, commentTop) * scrollY);
                if (alph <= 255) {
                    tv_headtitle.setTextColor(Color.argb(alph, 255, 255, 255));
                    fragment_qyxq_back.setAlpha(alph);
                    layout_nav_back.setAlpha((float) Tools.div((Tools.div(255, commentTop) * scrollY),255));
                } else {
                    tv_headtitle.setTextColor(Color.argb(255, 255, 255, 255));
                    fragment_qyxq_back.setAlpha(255);
                    layout_nav_back.setAlpha(1);
                }
            }
        });

    }

    private void initView() {

        fragment_qyxq_xBanner = (XBanner) findViewById(R.id.fragment_qyxq_xBanner);//轮播控件
        fragment_qyxq_title = (TextView) findViewById(R.id.fragment_qyxq_title);//标题
        fragment_qyxq_suoshu = (TextView) findViewById(R.id.fragment_qyxq_suoshu);//所属楼宇
        fragment_qyxq_guimo = (TextView) findViewById(R.id.fragment_qyxq_guimo);//企业规模
        fragment_qyxq_wangzhan = (TextView) findViewById(R.id.fragment_qyxq_wangzhan);//企业网站
        fragment_qyxq_addres = (TextView) findViewById(R.id.fragment_qyxq_addres);//企业地址
        fragment_qyxq_JYcontent = (TextView) findViewById(R.id.fragment_qyxq_JYcontent);//经营范围
        fragment_qyxq_JJcontent = (TextView) findViewById(R.id.fragment_qyxq_JJcontent);//企业简介
        fragment_qyxq_back = (ImageView) findViewById(R.id.fragment_qyxq_back);//返回按钮
        fragment_qyxq_back.setOnClickListener(this);
        layout_solve=(RelativeLayout) findViewById(R.id.layout_solve);
        tv_solve=(TextView) findViewById(R.id.tv_solve);
        tv_delete=(TextView) findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(this);
        layout_person=(LinearLayout) findViewById(R.id.layout_person);
        tv_name=(TextView) findViewById(R.id.tv_name);
        tv_sex=(TextView) findViewById(R.id.tv_sex);
        tv_phone=(TextView) findViewById(R.id.tv_phone);

        ll_mine_nav = (LinearLayout)findViewById(R.id.ll_mine_nav);
        ll_mine_nav_replace = (LinearLayout)findViewById(R.id.ll_mine_nav_replace);
        layout_nav_back = (RelativeLayout) findViewById(R.id.layout_nav_back);
        layout_nav_back.setAlpha(0);
        refreshscrollView = (HoveringScrollview) findViewById(R.id.mineScrollView);
        ll_item = (LinearLayout) findViewById(R.id.ll_item);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        layout_address=(RelativeLayout) findViewById(R.id.layout_address);
        layout_address.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_qyxq_back:
                finish();
                break;
            case R.id.tv_delete:
                contributorPresenter.getCompanyReleaseDelete(id,DELETEHOUSEDELETEFLAG);
                break;
            case R.id.layout_address:
                try {
                    LatLng starLng=new LatLng(myApplication.getLatitude(),myApplication.getLongitude());
                    OpenExternalMapAppUtils.openMapDirection(this, starLng.longitude+"", starLng.latitude+"",
                            myApplication.getLocationAddress(),
                            "", "", fragment_qyxq_addres.getText().toString().trim(), "智慧中国");
                }catch (Exception e)
                {

                }
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(DELETEHOUSEDELETEFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = (String) object;
                    if(jsonMessage.equals("41"))
                    {
                        myToast.showShort("删除成功");
                        Intent intent=getIntent();
                        setResult(1,intent);
                        finish();
                    }
                }else
                {
                    myToast.showShort("删除失败");
                }
            }else if (flag.equals(COMPANYDETAILFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    companyInfoDetailBean = JsonTools.jsonStringToObject(jsonMessage, CompanyInfoDetailBean.class);
                    if (companyInfoDetailBean != null) {
                        fragment_qyxq_title.setText(companyInfoDetailBean.getCompanyName());
                        if(companyInfoDetailBean.getBuildingName()==null || companyInfoDetailBean.getBuildingName().trim().isEmpty())
                        {
                            fragment_qyxq_suoshu.setText("所属楼宇："+"暂无");
                        }else {
                            fragment_qyxq_suoshu.setText("所属楼宇："+companyInfoDetailBean.getBuildingName());
                        }

                        fragment_qyxq_addres.setText(companyInfoDetailBean.getAddr());

                        if(companyInfoDetailBean.getRange()==null ||companyInfoDetailBean.getRange().trim().isEmpty())
                        {
                            fragment_qyxq_JYcontent.setText("暂无");
                        }else {
                            fragment_qyxq_JYcontent.setText(companyInfoDetailBean.getRange());
                        }

                        if(companyInfoDetailBean.getScale()==null || companyInfoDetailBean.getScale().trim().isEmpty() ||companyInfoDetailBean.getScale().equals("0"))
                        {
                            fragment_qyxq_guimo.setText("暂无");//企业规模
                        }else {
                            fragment_qyxq_guimo.setText(companyInfoDetailBean.getScale()+"人");//企业规模
                        }

                        if(companyInfoDetailBean.getWebsite()==null ||companyInfoDetailBean.getWebsite().toString().trim().isEmpty())
                        {
                            fragment_qyxq_wangzhan.setText("暂无");//企业网站
                        }else {
                            fragment_qyxq_wangzhan.setText(companyInfoDetailBean.getWebsite());//企业网站
                        }

                        if(companyInfoDetailBean.getIntro()==null ||companyInfoDetailBean.getIntro().trim().isEmpty())
                        {
                            fragment_qyxq_JJcontent.setText("暂无");//企业简介
                        }else {
                            fragment_qyxq_JJcontent.setText(companyInfoDetailBean.getIntro());//企业简介
                        }

                        fragment_qyxq_xBanner.setData(companyInfoDetailBean.getBanners(),null);
                        fragment_qyxq_xBanner.setPoinstPosition(XBanner.CENTER);//指示器居于中
                        fragment_qyxq_xBanner.setmAdapter(this);
                    }

                }
            }else if(flag.equals(COMPANYRELEASEDETAILFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    companyInfoDetailBean = JsonTools.jsonStringToObject(jsonMessage, CompanyInfoDetailBean.class);
                    if (companyInfoDetailBean != null) {
                        fragment_qyxq_title.setText(companyInfoDetailBean.getCompanyName());

                        if(companyInfoDetailBean.getBuildingName()==null || companyInfoDetailBean.getBuildingName().trim().isEmpty())
                        {
                            fragment_qyxq_suoshu.setText("所属楼宇："+"暂无");
                        }else {
                            fragment_qyxq_suoshu.setText("所属楼宇："+companyInfoDetailBean.getBuildingName());
                        }

                        fragment_qyxq_addres.setText(companyInfoDetailBean.getAddr());

                        if(companyInfoDetailBean.getRange()==null ||companyInfoDetailBean.getRange().trim().isEmpty())
                        {
                            fragment_qyxq_JYcontent.setText("暂无");
                        }else {
                            fragment_qyxq_JYcontent.setText(companyInfoDetailBean.getRange());
                        }

                        if(companyInfoDetailBean.getScale()==null || companyInfoDetailBean.getScale().trim().isEmpty()||companyInfoDetailBean.getScale().equals("0"))
                        {
                            fragment_qyxq_guimo.setText("暂无");//企业规模
                        }else {
                            fragment_qyxq_guimo.setText(companyInfoDetailBean.getScale()+"人");//企业规模
                        }

                        if(companyInfoDetailBean.getWebsite()==null ||companyInfoDetailBean.getWebsite().trim().isEmpty())
                        {
                            fragment_qyxq_wangzhan.setText("暂无");//企业网站
                        }else {
                            fragment_qyxq_wangzhan.setText(companyInfoDetailBean.getWebsite());//企业网站
                        }

                        if(companyInfoDetailBean.getIntro()==null ||companyInfoDetailBean.getIntro().trim().isEmpty())
                        {
                            fragment_qyxq_JJcontent.setText("暂无");//企业简介
                        }else {
                            fragment_qyxq_JJcontent.setText(companyInfoDetailBean.getIntro());//企业简介
                        }

                        fragment_qyxq_xBanner.setData(companyInfoDetailBean.getBanners(),null);
                        fragment_qyxq_xBanner.setPoinstPosition(XBanner.CENTER);//指示器居于中
                        fragment_qyxq_xBanner.setmAdapter(this);

                        if (companyInfoDetailBean.getState() == 0) {
                            tv_solve.setText("审核中");
                            tv_solve.setTextColor(Color.parseColor("#29D7F2"));
                        } else if (companyInfoDetailBean.getState() == 1) {
                            tv_solve.setText("发布成功");
                            tv_solve.setTextColor(Color.parseColor("#15CD1F"));
                        } else if (companyInfoDetailBean.getState() == 2) {
                            tv_solve.setText("被拒绝");
                            tv_solve.setTextColor(Color.parseColor("#FC5812"));
                        }
                        tv_name.setText(companyInfoDetailBean.getName());
                        tv_phone.setText(companyInfoDetailBean.getPhone());
                        if(companyInfoDetailBean.getGender()==0)
                        {
                            tv_sex.setText("女");
                        } else
                        {
                            tv_sex.setText("男");
                        }
                    }

                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void loadBanner(XBanner banner, Object model, View view, int position) {
        try {
            Glide.with(context).load(companyInfoDetailBean.getBanners().get(position)).into((ImageView) view);
        }catch (Exception e)
        {

        }
    }

}
